package com.app.view;

import android.R.bool;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.app.util.Utils_Log;
import com.app.util.cHighTimer;


public class CircleSurfaceView extends SurfaceView implements SurfaceHolder.Callback2, Runnable
{

	private static String TAG = CircleSurfaceView.class.getSimpleName();
	

	private Paint mPaintCircle;
	private Point mPointCenter;
	private int mRadius = 50;
	
	private Context mContext;
	private Paint mPaintRing;
	
	private int mCircleRadius = 10;
	private RectF mRectRing = new RectF();
	
	private int Radius = mRadius;
	private int alpha = 0xff;
	
	
	private Rect mSize = new Rect();
	private cHighTimer mHighTimer = new cHighTimer();
	private int mBackgroundColor = Color.GRAY; // 背景色
	
	
	SurfaceHolder mHolder;
	
	boolean bRun = false;
	
	
	public CircleSurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		mContext = context;
		
		
		mHolder = this.getHolder();
		mHolder.addCallback(this);
	
		
	}
	
	/*
	public CircleSurfaceView(Context context, AttributeSet set){
		super(context, set);
		
		mContext = context;
		
		mHolder = this.getHolder();
		mHolder.addCallback(this);
		
	}
	*/
	
	
	
	///
	public void SetPointCenter(Point center){
		mPointCenter = center;
		
	}
	
	public void SetBackground(int BackgroundColor){
		mBackgroundColor = BackgroundColor;
	}
	///
	
	
	void init(){
		
		if(mPointCenter == null){
			mPointCenter = new Point(200,200);
		}
		
		mPaintCircle = new Paint();
		mPaintCircle.setColor( Color.parseColor("#eeeeee") );
		
		mPaintRing = new Paint();
		mPaintRing.setColor( Color.parseColor("#111111"));
		mPaintRing.setStyle(Style.STROKE);
		mPaintRing.setStrokeWidth(5);
		
		
		mHighTimer.start_counter();
	 
	}
	

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		bRun = true;
		init();
		new Thread(this).start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		bRun = false;
	}

	@Override
	public void surfaceRedrawNeeded(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		while(bRun){
			float elapsed = mHighTimer.get_elapsed_time();
			if( mHighTimer.controlFPS_Return(elapsed, 33) ){
				synchronized (mHolder) {
					Update();
					Draw();
				}	
			}
			
		}
		
		
	}	
	
	
	
	void Update(){
		
		if(Radius > mRadius*3 || alpha < 0x10){
			Radius = mRadius;
			
			alpha = 0xff;
		}
		else{
			Radius+=1;
		}
    	
		 
    	mRectRing = new RectF();      
    
    	mRectRing.left =mPointCenter.x-Radius;
    	mRectRing.top =mPointCenter.y-Radius;
    	mRectRing.right =mPointCenter.x+Radius;
    	mRectRing.bottom =mPointCenter.y+Radius;

    // 	Utils_Log.d(TAG, "mRectRing: "+mRectRing.toString() );
    	
    	alpha -=5;
    	mPaintRing.setColor(  Color.argb(alpha, 11, 11, 11) );
	}
	
	
	
	void Draw(){
		
		/*
		Rect size = new Rect();
		size.left =(int) mRectRing.left;
		size.top = (int) mRectRing.top;
		size.right = (int) mRectRing.right;
		size.bottom = (int) mRectRing.bottom;
		Canvas canvas = mHolder.lockCanvas(size);
		*/
		Canvas canvas = mHolder.lockCanvas();
		if(canvas == null)
			return;
		
		canvas.drawColor(mBackgroundColor); 
		
    	
		canvas.drawCircle(mPointCenter.x, mPointCenter.y, mRadius, mPaintCircle);
		canvas.drawOval(mRectRing, mPaintRing);
    	
		
		mHolder.unlockCanvasAndPost(canvas);
		
		
	}
	
 
	
 
	
};

